
#include <BMouseEvent>
#include <BTableItem>
#include <BPainter>

#include "ImageLayer.h"
#include "LayerTableBoard.h"

LayerTableBoard::LayerTableBoard(BWidget* parent) : BTableWidget(parent)
{
	_pressedRow = -1;
	_hoveredRow = -1;
	_titleEdit = new BLineEdit(this);
	_titleEdit->hide();

	connect(this, Signal_Freshed, &LayerTableBoard::slotTableWidgetFreshed);
	connect(this, Signal_ItemClicked, &LayerTableBoard::slotTableItemClicked);

	connect(_titleEdit, Signal_Focused, &LayerTableBoard::slotTitleEditFocused);
	connect(_titleEdit, Signal_EditFinished, &LayerTableBoard::slotTitleEditFinished);

}
LayerTableBoard::~LayerTableBoard()
{

}

void LayerTableBoard::slotTableWidgetFreshed(BObject* object, const BValue& value)
{
	if (_titleEdit->visible())
	{
		if (BTableItem* item = _titleEdit->asset())
		{
			_titleEdit->setRect(item->rect());
		}
	}
}
void LayerTableBoard::slotTableItemClicked(BObject* object, const BValue& value)
{
	if (_titleEdit->asset() != value)
	{
		_titleEdit->setAsset(value);
	}
	else if (BTableItem* item = value)
	{
		_titleEdit->setRect(item->rect());
		_titleEdit->setText(item->text());
		_titleEdit->raise();
		_titleEdit->show();
	}
}
void LayerTableBoard::slotTitleEditFocused(BObject* object, const BValue& value)
{
	if (value == false)
	{
		_titleEdit->setAsset(BValue());
		_titleEdit->hide();
	}
}
void LayerTableBoard::slotTitleEditFinished(BObject* object, const BValue& value)
{
	BTableItem* item = _titleEdit->asset();
	BWidget* widget = this->rowAsset(item->row());
	widget->setTitle(_titleEdit->text());
	item->setText(_titleEdit->text());
}

void LayerTableBoard::paintEvent(const BEvent& event)
{
	BTableWidget::paintEvent(event);
	BPainter painter(this);
	if (_pressedRow >= 0)
	{
		painter.setColor(Color_Border, State_Checked);
		painter.drawRect(this->rowRect(_pressedRow));
	}
	if (_hoveredRow >= 0)
	{
		painter.setColor(Color_Indicator);
		if (_hoveredRow == this->rowCount())
		{
			BRect rowRect = this->rowRect(_hoveredRow - 1);
			painter.drawLine(rowRect.leftBottom(), rowRect.rightBottom());
		}
		else
		{
			BRect rowRect = this->rowRect(_hoveredRow);
			painter.drawLine(rowRect.leftTop(), rowRect.rightTop());
		}
	}
}

void LayerTableBoard::mousePressEvent(const BMouseEvent& event)
{
	BTableWidget::mousePressEvent(event);
	if (event.button() == Button_Left)
	{
		_pressedRow = this->rowAt(event.pos());
		_hoveredRow = -1;
	}
}
void LayerTableBoard::mouseReleaseEvent(const BMouseEvent& event)
{
	BTableWidget::mouseReleaseEvent(event);
	if (_pressedRow >= 0 && _hoveredRow >= 0 && _pressedRow != _hoveredRow)
	{
		BWidget* widget = this->rowAsset(_pressedRow);
		BArray<BTableItemHolder> items;
		for (int c = 0; c < this->columnCount(); c++)
		{
			items.append(this->item(_pressedRow, c));
		}
		this->removeRow(_pressedRow);
		this->insertRow(_hoveredRow);
		this->setRowAsset(_hoveredRow, widget);
		for (int c = 0; c < this->columnCount(); c++)
		{
			this->setItem(_hoveredRow, c, items[c]);
		}
		int beginRow = bMin(_pressedRow, _hoveredRow);
		for (int r = beginRow; r < this->rowCount(); r++)
		{
			this->setRowLabel(r, r);
		}
		emit("layer-arranged", beginRow);
	}
	_pressedRow = -1;
	_hoveredRow = -1;
}
void LayerTableBoard::mouseMoveEvent(const BMouseEvent& event)
{
	BTableWidget::mouseMoveEvent(event);
	if (_pressedRow >= 0)
	{
		_hoveredRow = this->rowAt(event.pos());
	}
}
